package logic

import (
	"context"
	"encoding/json"
	"errors"
	"github.com/zeromicro/go-zero/core/logx"
	"go-zero-project-02/model_study/user_gorm/api/internal/svc"
	"go-zero-project-02/model_study/user_gorm/api/internal/types"
	"go-zero-project-02/model_study/user_gorm/models"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginRequest) (resp string, err error) {
	//查询
	userName := req.UserName
	password := req.Password
	var user models.UserModel
	err = l.svcCtx.Bb.Take(&user, "username = ? and password = ?", userName, password).Error
	if err != nil {
		return "", errors.New("记录不存在")
	}
	marshal, _ := json.Marshal(user)
	return string(marshal), nil

	//add
	//err = l.svcCtx.Bb.Create(&models.UserModel{
	//	Username:      userName,
	//	Password:      password,
	//	RegisterTime:  time.Now(),
	//	LastLoginTime: time.Now(),
	//}).Error
	//logx.Infof("新增user-id:%v", user.Id)
	//return "success", nil

}
